{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import json\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('Masks/part2.json', 'r') as f:\n",
    "    D = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Folder = set(np.arange(1,101))-set(np.array([ 1,  4,  6,  9, 11, 15, 16, 17, 22, 24, 27, 28, 29, 30, 34, 35, 37,38, 40, 41, 42, 44, 46, 52, 54, 57, 63, 68, 71, 75, 76, 79, 80, 81,82, 84, 89, 96]))\n",
    "Folder = list(Folder)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0\n",
    "names = [\"car\",\"bus\",\"truck\"]\n",
    "\n",
    "for frame in D:\n",
    "#     print(frame['filename'],frame['filename'].split('/')[4])\n",
    "    if frame['filename'].split('/')[4] == str(Folder[i]):\n",
    "\n",
    "        file1 = open(\"Masks/\" +str(Folder[i]) + \".txt\",\"a\")\n",
    "#         print(frame['filename'])\n",
    "        for bound in frame['objects']:\n",
    "            if bound['name'] in names:\n",
    "                frame_n = frame['filename'].split('/')[5].split('.')[0]\n",
    "#                 print(frame_n)\n",
    "                a = -1\n",
    "                b = bound['relative_coordinates']['center_x']*800\n",
    "                c = bound['relative_coordinates']['center_y']*410\n",
    "                d = bound['relative_coordinates']['width']*800\n",
    "                e = bound['relative_coordinates']['height']*410\n",
    "                f = bound['confidence']\n",
    "                g = -1\n",
    "                h = -1\n",
    "                j = -1\n",
    "                line = [frame_n,a,b,c,d,e,f,g,h,j]\n",
    "                for l in line:\n",
    "                    file1.write(str(l)+\",\")\n",
    "                file1.write(\"\\n\")\n",
    "        file1.close()\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('Masks/part1.json', 'r') as f:\n",
    "    D = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  6,  9, 11, 15, 16, 17, 22, 24, 27, 28, 29, 30, 34, 35, 37,\n",
       "       38, 40, 41, 42, 44, 46, 52, 54, 57, 63, 68, 71, 75, 76, 79, 80, 81,\n",
       "       82, 84, 89, 96])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([ 1,  4,  6,  9, 11, 15, 16, 17, 22, 24, 27, 28, 29, 30, 34, 35, 37,38, 40, 41, 42, 44, 46, 52, 54, 57, 63, 68, 71, 75, 76, 79, 80, 81,82, 84, 89, 96])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0\n",
    "names = [\"car\",\"bus\",\"truck\"]\n",
    "\n",
    "for frame in D:\n",
    "#     print(frame['filename'],frame['filename'].split('/')[4])\n",
    "    if frame['filename'].split('/')[4] == str(Folder[i]):\n",
    "\n",
    "        file1 = open(\"Masks/\" +str(Folder[i]) + \".txt\",\"a\")\n",
    "#         print(frame['filename'])\n",
    "        for bound in frame['objects']:\n",
    "            if bound['name'] in names:\n",
    "                frame_n = frame['filename'].split('/')[5].split('.')[0]\n",
    "#                 print(frame_n)\n",
    "                a = -1\n",
    "                b = bound['relative_coordinates']['center_x']*800\n",
    "                c = bound['relative_coordinates']['center_y']*410\n",
    "                d = bound['relative_coordinates']['width']*800\n",
    "                e = bound['relative_coordinates']['height']*410\n",
    "                f = bound['confidence']\n",
    "                g = -1\n",
    "                h = -1\n",
    "                j = -1\n",
    "                line = [frame_n,a,b,c,d,e,f,g,h,j]\n",
    "                for l in line:\n",
    "                    file1.write(str(l)+\",\")\n",
    "                file1.write(\"\\n\")\n",
    "        file1.close()\n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
